package com.example.automanagement.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.automanagement.dto.UserDto;
import com.example.automanagement.entity.*;
import com.example.automanagement.service.UserService;
import com.example.automanagement.util.JwtUtils;
import com.example.automanagement.util.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    //用户登录
    @PostMapping(value = "/userLogin")
    public Result userLogin(@RequestParam("loginUserId") String userId, @RequestParam("loginUserPassword") String userPassword) {
        User user = userService.userLogin(userId, userPassword);
        if (user != null) {
            Map<String, Object> claims = new HashMap<>();
            List<UserRole> userRoles = userService.getUserRole(userId);
            claims.put("userId", user.getUserId());
            claims.put("userName", user.getUserName());
            claims.put("userRoles", userRoles);
            String jwt = JwtUtils.generateJwt(claims);

            return Result.success(jwt, "登录成功!");
        } else {
            return Result.error("500", "账号或密码错误!");
        }
    }

    //获取用户角色
    @GetMapping(value = "/getUserRole")
    public Result getUserRole(@RequestParam("getUserRoleUserId") String userId){
        try{
            List<UserRole> userRoleList = userService.getUserRole(userId);
            return Result.success(userRoleList,"查询成功");
        } catch (Exception e) {
            return Result.error("500","查询失败");
        }
    }

    //用户修改个人信息
    @PutMapping(value = "/userUpdateInformation")
    public Result userUpdateInformation(User user){
        try{
            userService.userUpdateInformation(user);
            return Result.success("修改成功");
        } catch (Exception e) {
            return Result.error("500","修改失败");
        }
    }


    //用户修改密码
    @PutMapping(value = "/userUpdatePassword")
    public Result userUpdatePassword(@RequestParam("userId")String userId,@RequestParam("oldPassword")String oldPassword,@RequestParam("newPassword")String newPassword){
        try{
            userService.userUpdatePassword(userId,oldPassword,newPassword);
            return Result.success("修改成功");
        } catch (Exception e){
            return Result.error("500","修改失败");
        }

    }

    //综合办修改用户信息
    @PutMapping(value = "/adminUpdateUser")
    public Result adminUpdateUser(User user){
        try{
            userService.adminUpdateUser(user);
            return Result.success("修改成功");
        } catch (Exception e) {
            return Result.error("500","修改失败");
        }
    }

    //删除用户角色
    @DeleteMapping(value = "/deleteUserRole")
    public Result deleteUserRole(@RequestParam("userId") String userId){
        try {
            userService.deleteUserRole(userId);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("500","删除成功");
        }
    }


    //新增用户
    @PostMapping(value = "/addUser")
    public Result addUser(User user){
        try{
            userService.addUser(user);
            return Result.success("添加成功");
        } catch (Exception e) {
            return Result.error("500","添加失败");
        }
    }

    //为用户新增角色
    @PostMapping(value = "/addUserRole")
    public Result addUserRole(@RequestBody List<UserRole> userRoleList){
        try {
            userService.addUserRole(userRoleList);
            return Result.success("新增成功");
        } catch (Exception e) {
            return Result.error("500","新增失败");
        }
    }

    //删除用户
    @DeleteMapping(value = "/deleteUser")
    public Result deleteUser(@RequestParam("userIdDelete") String userId){
        try {
            userService.deleteUser(userId);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("500","删除失败");
        }
    }


    //获取角色菜单
    @GetMapping(value = "/getRoleBusiness")
    public Result getRoleBusiness(@RequestParam("BusinessUserRole") String userRole){
        try {
            List<RoleBusinessMenu> roleBusinessMenuList =userService.getRoleBusiness(userRole);

            return Result.success(roleBusinessMenuList,"查询成功");
        } catch (Exception e) {
            return Result.error("500","查询失败！");
        }
    }

    //获取全部角色
    @GetMapping(value = "/getAllRoles")
    public Result getAllRoles(){
        try{
            List<Role> roleList = userService.getAllRoles();
            return Result.success(roleList,"查询成功");
        } catch (Exception e) {
            return Result.error("500","查询失败");
        }
    }

    //获取可以添加的菜单（不在该角色菜单中的菜单）
    @GetMapping(value = "/getOtherMenus")
    public Result getOtherMenus(@RequestParam("userRoleGetOthers") String userRole){
        try {
            List<RoleBusinessMenu> roleBusinessMenuList = userService.getOtherMenus(userRole);
            return Result.success(roleBusinessMenuList,"查询成功");
        } catch (Exception e) {
            return Result.error("500","查询失败");
        }
    }

    //为角色添加可以访问的菜单
    @PostMapping(value = "/addRoleMenu")
    public Result addRoleMenu(@RequestBody List<RoleBusiness> roleBusinessMenuList){
        try {
            userService.addRoleMenu(roleBusinessMenuList);
            return Result.success("添加成功");
        } catch (Exception e) {
            return  Result.error("500","添加失败");
        }
    }

    //新增角色
    @PostMapping(value = "/addRole")
    public Result addRole(@RequestParam("roleAdd")String roleName){
        try {
            userService.addRole(roleName);
            return Result.success("新增成功");
        } catch (Exception e) {
            return Result.error("500","新增失败");
        }
    }

    //删除角色
    @DeleteMapping(value = "/deleteRole")
    public Result deleteRole(@RequestParam("roleDelete") String roleName){
        try{
            userService.deleteRole(roleName);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("500","删除失败");
        }
    }

    //删除角色对应菜单
    @DeleteMapping(value = "/deleteRoleMenu")
    public Result deleteRoleMenu(@RequestParam("userRoleDelete")String userRole, @RequestParam("businessIdDelete")int businessId){
        try {
            userService.deleteRoleMenu(userRole,businessId);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error("500","删除失败");
        }
    }

    @GetMapping(value = "/getAllUser")
    public Result getAllUser(){
        try{
            List<UserDto> userList = userService.getAllUser();
            return Result.success(userList,"查询成功");
        } catch (Exception e) {
            return Result.error("500","查询失败");
        }
    }
    @GetMapping(value = "/getAllDepartment")
    public Result getAllDepartment(){
        try{
            List<String> departmentList = userService.getAllDepartment();
            return Result.success(departmentList,"查询成功");
        } catch (Exception e) {
            return Result.error("500","查询失败");
        }
    }
    @GetMapping(value = "/getAllPosition")
    public Result getAllPosition(){
        try{
            List<String> positionList = userService.getAllPosition();
            return Result.success(positionList,"查询成功");
        } catch (Exception e) {
            return Result.error("500","查询失败");
        }
    }
    //个人信息查询端口，参数传入id
    @GetMapping(value = "/getUserInfoById")
    public Result getUserInfoById(@RequestParam("userId")String userId){
        try{
            List<User> userList = userService.getUserInfoById(userId);
            return Result.success(userList,"查询成功");
        } catch (Exception e) {
            return Result.error("500","查询失败");
        }
    }
}
